package com.tangbuzhi.factory_method;
/**
 * 工厂方法模式：普通工厂，多个工厂，静态工厂
 */
public class FactoryMethod {
    /**
     * 产品类接口
     */
    public interface Sender {
        void send();
    }

    /**
     * 具体产品实现类
     */
    public static class MailSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is mail sender!");
        }
    }

    /**
     * 具体产品实现类
     */
    public static class QQSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is qq sender!");
        }
    }

    /**
     * 普通工厂
     */
    public static class SendFactory {
        public Sender create(String type) {
            if ("mail".equals(type)) {
                return new MailSender();
            } else if ("qq".equals(type)) {
                return new QQSender();
            } else {
                System.out.println("type is not match");
                return null;
            }
        }
    }

    /**
     * 多个工厂
     */
    public static class MultiFactory {
        public MailSender createMail() {
            return new MailSender();
        }

        public QQSender createQQ() {
            return new QQSender();
        }
    }

    /**
     * 静态工厂
     */
    public static class StaticFactory {
        public static MailSender createMail() {
            return new MailSender();
        }

        public static QQSender createQQ() {
            return new QQSender();
        }
    }

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        SendFactory sendFactory = new SendFactory();
        sendFactory.create("qq").send();
        sendFactory.create("mail").send();
        sendFactory.create("bmw");

        System.out.println("============below is multi factory================");
        MultiFactory multiFactory = new MultiFactory();
        multiFactory.createMail().send();
        multiFactory.createQQ().send();

        System.out.println("=============below is static factory===============");
        StaticFactory.createMail().send();
        StaticFactory.createQQ().send();

        /**
         * 输出结果：
         this is qq sender!
         this is mail sender!
         type is not match
         ============below is multi factory================
         this is mail sender!
         this is qq sender!
         =============below is static factory===============
         this is mail sender!
         this is qq sender!
         */
    }
}
